package com.liuyang.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuyang.common.Result;
import com.liuyang.entity.Account;
import com.liuyang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RegisterController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/register")
    public Result register(Account account) {
        int i = accountService.count(new QueryWrapper<Account>().eq("accountName", account.getAccountName()));
        if(i > 0) {
            return Result.succ("此账号已注册");
        }
        Snowflake snowflake = new Snowflake(2,3);
        String id = snowflake.nextIdStr();
        account.setAccountPassword(SecureUtil.md5(account.getAccountPassword()));
        account.setId(id);
        account.setAccountStatus("1");
        account.setAccountType("0");
        accountService.save(account);
        return Result.succ("注册成功");
    }

}
